package com.woniu.secondhand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.ResultCode;
import com.woniu.common.exception.LifeException;
import com.woniu.secondhand.constant.SecondhandMessageConstant;
import com.woniu.secondhand.constant.SecondhandOrderConstant;
import com.woniu.secondhand.entity.dto.CommentAddDTO;
import com.woniu.secondhand.entity.dto.MessageAddDTO;
import com.woniu.secondhand.mapper.SecondhandCommentMapper;
import com.woniu.secondhand.pojo.SecondhandComment;
import com.woniu.secondhand.pojo.SecondhandOrder;
import com.woniu.secondhand.pojo.SecondhandProduct;
import com.woniu.secondhand.service.ISecondhandCommentService;
import com.woniu.secondhand.service.ISecondhandMessageService;
import com.woniu.secondhand.service.ISecondhandOrderService;
import com.woniu.secondhand.service.ISecondhandProductService;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@Service
public class SecondhandCommentServiceImpl extends ServiceImpl<SecondhandCommentMapper, SecondhandComment> implements ISecondhandCommentService {

    @Autowired
    private SecondhandCommentMapper commentMapper;
    @Autowired
    private ISecondhandProductService productService;
    @Autowired
    private ISecondhandOrderService orderService;
    @Autowired
    private ISecondhandMessageService messageService;

    @Override
    public SecondhandComment getByUserIdAndProductId(Integer userId, Integer productId) {
        QueryWrapper<SecondhandComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("product_id", productId);
        SecondhandComment comment = commentMapper.selectOne(wrapper);
        return comment;
    }

    @Override
    public SecondhandComment addComment(CommentAddDTO commentAddDTO, UserLoginVO userLoginVO) {
        SecondhandComment selectComment = getByUserIdAndProductId(userLoginVO.getId(), commentAddDTO.getProductId());
        if (selectComment != null) {
            throw new LifeException(ResultCode.DATA_ALREADY_EXISTED.getMessage(), ResultCode.DATA_ALREADY_EXISTED);
        }
        SecondhandOrder order = orderService.getByProductIdAndStatusEqualsReceiveSuccess(commentAddDTO.getProductId());
        if (order == null) {
            throw new LifeException(ResultCode.RESULT_DATA_NONE.getMessage(), ResultCode.RESULT_DATA_NONE);
        }
        if (!Objects.equals(order.getStatus(), SecondhandOrderConstant.RECEIVE_SUCCESS)) {
            throw new LifeException(ResultCode.RESULT_DATA_NONE.getMessage(), ResultCode.RESULT_DATA_NONE);
        }
        SecondhandComment comment = new SecondhandComment();
        SecondhandProduct product = productService.getById(commentAddDTO.getProductId());
        comment.setUserId(userLoginVO.getId());
        comment.setProductId(product.getId());
        comment.setProducerId(product.getProducerId());
        comment.setContent(commentAddDTO.getContent());
        commentMapper.insert(comment);
        MessageAddDTO messageAddDTO = new MessageAddDTO();
        messageAddDTO.setProductId(product.getId());
        messageAddDTO.setMessageType(SecondhandMessageConstant.COMMENT_SUCCESS_MESSAGE);
        messageService.addMessage(messageAddDTO, userLoginVO);
        order.setStatus(SecondhandOrderConstant.COMMENT_SUCCESS);
        order.setCommentTime(new Date());
        orderService.updateById(order);
        return comment;
    }
}
